import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:fluro/fluro.dart';

import 'package:flutterpicgo/routers/application.dart';
import 'package:flutterpicgo/utils/local_notification.dart';
import 'package:flutterpicgo/resources/theme_colors.dart';
import 'package:flutterpicgo/routers/routers.dart';
import 'package:flutterpicgo/model/theme_state.dart';
import 'package:flutterpicgo/utils/db_provider.dart';

ThemeState themeState;


Future<void> main() async  {

  /// needed if you intend to initialize in the `main` function
  WidgetsFlutterBinding.ensureInitialized();
  final provider = DbProvider();
  await provider.init();

  /// notification initialization
  LocalNotificationUtil.getInstance().initialization();

  /// theme initialization
  themeState = ThemeState();

  runApp(App());
}


class App extends StatefulWidget {

  App() {
    final router = new FluroRouter();
    Routes.configureRoutes(router);
    Application.router = router;
  }

  @override
  _AppState createState() => _AppState();

}

class _AppState extends State<App> {
  @override
  Widget build(BuildContext context) {
   return MultiProvider(
     providers: [
       ChangeNotifierProvider(
         create: (context) => themeState,
       ),
     ],
     child: Consumer<ThemeState>(
       builder: (context,state,child) {

         return state.currentMode == ThemeMode.system
             ? MaterialApp(
           debugShowCheckedModeBanner: false,
           theme: lightThemeData,
           darkTheme: darkThemeData,
           initialRoute: '/',
           onGenerateRoute: Application.router.generator,
         )
             : MaterialApp(
           debugShowCheckedModeBanner: false,
           theme: state.currentMode == ThemeMode.light
               ? lightThemeData
               : darkThemeData,
           initialRoute: '/',
           onGenerateRoute: Application.router.generator,
         );
       },
     ),
   );
  }

}

